package infra.jenkins;

import domain.model.project.Project;
import domain.model.project.ProjectId;
import domain.model.project.ProjectType;

import java.util.Map;

/**
 * @author Gregory Boissinot
 */
public class JenkinsRule {

    public static final String JOB_EXTENSION = "JOB_EXTENSION";

    private static JenkinsRule INSTANCE;

    private JenkinsRule() {
    }

    public static JenkinsRule getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new JenkinsRule();
        }
        return INSTANCE;
    }

    public String getJobName(Project project) {

        final ProjectId projectId = project.getProjectId();
        final String baseJobName = projectId.getOrganization() + "." + projectId.getName();

        final String jobNameExtension = getJobNameExtension(project.getProjectType());
        if (jobNameExtension == null) {
            return baseJobName;
        }

        return baseJobName + "_" + jobNameExtension;
    }

    private String getJobNameExtension(ProjectType projectType) {
        if (projectType == null) {
            return null;
        }

        final Map<String, Object> metaInformation = projectType.getMetaInformation();
        if (metaInformation == null) {
            return null;
        }

        return (String) metaInformation.get(JOB_EXTENSION);
    }

}
